package com.itcc.controller;

import com.itcc.entity.Cart;
import com.itcc.entity.User;
import com.itcc.service.CartService;
import com.itcc.service.impl.CartServiceImpl;
import com.itcc.utils.Constants;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/cart")
public class CartController extends BaseServlet{

    public String create(HttpServletRequest request, HttpServletResponse response) throws SQLException, InvocationTargetException, IllegalAccessException {

        //判断是否登录
        HttpSession session = request.getSession();
        User user=(User)session.getAttribute("loginUser");
        if(user==null){
            request.setAttribute("msg","请先登录！");
            return Constants.REDIRECT+"/login.jsp";
        }

        //获取uid，pid
        String pid=request.getParameter("pid");
        int uid= user.getUid();

        CartService cartService=new CartServiceImpl();
        cartService.createCart(uid,pid);

        return Constants.FORWARD+"/cartSuccess.jsp";
    }
    public String  show (HttpServletRequest request,HttpServletResponse response) throws SQLException, InvocationTargetException, IllegalAccessException {

        //判断是否登录
        HttpSession session = request.getSession();
        User user=(User)session.getAttribute("loginUser");
        if(user==null){
            request.setAttribute("msg","请先登录！");
            return Constants.REDIRECT+"/login.jsp";
        }

        int uid = user.getUid();
        CartService cartService=new CartServiceImpl();
        List<Cart> list=cartService.findAll(uid);

        request.setAttribute("list",list);
        return Constants.FORWARD+"/cart.jsp";
    }

    public String delete(HttpServletRequest request,HttpServletResponse response) throws SQLException {

        String cid = request.getParameter("cid");

        CartService cartService=new CartServiceImpl();
        cartService.deleteCartByCid(cid);

        return Constants.FORWARD+"/cart?method=show";
    }

    public String update(HttpServletRequest request,HttpServletResponse response) throws SQLException {

        String cid = request.getParameter("cid");
        String price = request.getParameter("price");
        String cnum = request.getParameter("cnum");

        CartService cartService=new CartServiceImpl();
        cartService.updateCartByCid(cid,price,cnum);

        return Constants.FORWARD+"/cart?method=show";
    }
    public String clear(HttpServletRequest request,HttpServletResponse response) throws SQLException {

        String uid = request.getParameter("uid");
        CartService cartService=new CartServiceImpl();
        cartService.clearCart(uid);

        return Constants.FORWARD+"/cart?method=show";
    }
}
